package com.loveyukari.service;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loveyukari.model.Auth;
import com.loveyukari.model.LiwuStyle;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
*
*yangshaohua
*2017年7月26日下午4:19:47
**/
@Service
public class DrawLiwuService {

	private Logger log = LoggerFactory.getLogger(DrawLiwuService.class);
	
	@Autowired
	private AuthService authService;
	@Autowired
	private LiwuService liwuService;
	
	private Date startTime;
	private int gfid;
	private boolean drawLiwuFlag;
	private Date endTime;
	private Set<Integer> drawAuthSet;
	private List<Integer> drawAuthIds;
	private int count = 1;
	private List<LiwuStyle> liwuStyleList;
	
	public List<LiwuStyle> loadLiwuStyle(){
		this.liwuStyleList = new ArrayList<LiwuStyle>();
		try {
			CloseableHttpClient httpClient = HttpClients.createDefault();
			HttpGet httpGet = new HttpGet("https://www.douyu.com/196");
			CloseableHttpResponse response = httpClient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String entityStr = EntityUtils.toString(entity);
				String startStr = "$ROOM.giftBatterConfig = ";
				int indexGift = entityStr.indexOf(startStr);
				JSONObject jsonObject = JSONObject.fromObject(entityStr.substring(indexGift+startStr.length(), entityStr.indexOf(";", indexGift)));
				JSONArray gfids = jsonObject.names();
				for (int j = 0; j < gfids.size(); j++) {
					String gfidStr = gfids.getString(j);
					int gfid = Integer.parseInt(gfidStr);
					LiwuStyle liwuStyle = liwuService.getLiwuStyleByGfid(gfid);
					this.liwuStyleList.add(liwuStyle);
				}
			}
		} catch (IOException e) {
			log.error(e.getMessage(),e);
		}
		return this.liwuStyleList;
	}
	
	
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	public String start(int gfid,int count){
		this.startTime = new Date();
		log.info("start draw chatmsg "+startTime);
		this.gfid = gfid;
		this.count = count;
		this.drawAuthSet = new HashSet<Integer>();
		this.drawAuthIds = new ArrayList<Integer>();
		this.drawLiwuFlag = true;
		return sdf.format(this.startTime);
	}
	
	public JSONObject stop(){
		this.drawLiwuFlag = false;
		this.endTime = new Date();
		JSONArray authArray = new JSONArray();
		if (!this.drawAuthSet.isEmpty()){
			Random random = new Random();
			List<Integer> list = new ArrayList<Integer>(this.drawAuthSet);
			while(true){
				if (list.size() == 0){
					break;
				}
				int index = random.nextInt(list.size());
				Integer uid = list.get(index);
				Auth auth = authService.getCacheAuth(uid);
				if (auth != null){
					this.drawAuthIds.add(auth.getId());
					JSONObject jsonAuth = new JSONObject();
					jsonAuth.put("id", auth.getId());
					jsonAuth.put("name", auth.getName());
					authArray.add(jsonAuth);
					list.remove(index);
				}
				if (this.drawAuthIds.size() == count){
					break;
				}
			}
		}
		log.info("end draw chatmsg "+this.endTime+", authIds="+this.drawAuthIds+",size="+this.drawAuthSet.size());
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("endTime", sdf.format(this.endTime));
		jsonObject.put("authArray", authArray);
		jsonObject.put("size", this.drawAuthSet.size());
		return jsonObject;
	}
	
	public void clear(){
		this.drawLiwuFlag = false;
		this.startTime = null;
		this.endTime = null;
		this.drawAuthSet = null;
		this.drawAuthIds = null;
	}
	
	public JSONObject getStatus(){
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("drawChatmsgFlag", this.drawLiwuFlag);
		jsonObject.put("count", this.count);
		jsonObject.put("gfid", this.gfid);
		if (liwuStyleList == null){
			this.loadLiwuStyle();
		}
		jsonObject.put("liwuStyleList", this.liwuStyleList);
		if (this.startTime != null){
			jsonObject.put("startTime", sdf.format(this.startTime));
		}
		if (this.endTime != null){
			jsonObject.put("endTime", sdf.format(this.endTime));
		}
		if (this.drawAuthSet != null && !this.drawLiwuFlag){
			jsonObject.put("size", this.drawAuthSet.size());
		}
		if (this.drawAuthIds != null && !this.drawLiwuFlag){
			JSONArray jsonArray = new JSONArray();
			for (Integer id : this.drawAuthIds) {
				Auth auth = authService.getCacheAuth(id);
				JSONObject jsonAuth = new JSONObject();
				jsonAuth.put("id", auth.getId());
				jsonAuth.put("name", auth.getName());
				jsonArray.add(jsonAuth);
			}
			jsonObject.put("authArray", jsonArray);
		}
		return jsonObject;
	}
	
	public boolean isDrawLiwuStart(){
		return this.drawLiwuFlag;
	}
	
	public boolean isDrawLiwu(int gfid){
		return this.gfid == gfid;
	}
	
	public void addAuth(Integer id){
		this.drawAuthSet.add(id);
	}
}
